Unit 20: Clustering (Agglomerative) and Evaluation Metrics
1. Introduction
This unit introduces Clustering, with a focus on hierarchical clustering, particularly Agglomerative Clustering and its linkage methods: single, complete, and average.
Learning Objectives
Distinguish between Partitional and Hierarchical clustering paradigms
Explain the Agglomerative Clustering algorithm step-by-step
Apply Single (MIN), Complete (MAX), and Average Linkage distance formulas
Compare Agglomerative vs Divisive hierarchical clustering
Today's Agenda
Recap of the Previous Lecture
Discussion on Challenge 2
Neural Network Summary
Python Demo of Hyperparameters of NN
Introduction to Clustering
Partitional vs Hierarchical Clustering
Agglomerative Clustering
Single, Complete and Average Linkages
2. Theory
2.1 Introduction to Clustering
Cluster Analysis is the process of finding similarities among data objects and grouping them into clusters. It is an unsupervised learning method, so no predefined classes are used. A canonical example is Google News, which groups similar news stories.
2.2 The Notion of a Cluster Can Be Ambiguous
Depending on your viewpoint, the same dataset could be divided into 4 clusters, 2 clusters, or 6 clusters. The "right" answer depends on the context and application, which is one reason clustering evaluation can be subtle.
2.3 Two Major Clustering Paradigms
Partitional Clustering
Hierarchical Clustering
Definition: Division of data objects into non-overlapping subsets (clusters) such that each object is in exactly one subset.
Divides data into K non-overlapping partitions.
K-Means: Requires K to be specified in advance.
DBSCAN: Automatically determines number of clusters.
Example visual:
Definition: A set of nested clusters organized as a hierarchical tree.
Can be visualized as a dendrogram.
There is no need to specify K in advance; any number of clusters can be obtained by cutting the tree at an appropriate level.
Side-by-Side Comparison
Aspect
Hierarchical Clustering
Partitional Clustering (e.g., K-Means)
Number of Clusters
No need to specify in advance
Some require K (K-Means, K-Medoids); some discover K (DBSCAN, OPTICS, ART)
Result
Dendrogram showing nested clusters
Single partition of data
Flexibility
Can obtain any number of clusters by cutting dendrogram
Fixed K clusters
Dataset Size
Best for small to medium datasets (< 10,000 points)
Suitable for large datasets (millions of points)
Common Applications
Biological taxonomy, document organization, gene sequence analysis, social network analysis
Start with each data point as its own singleton cluster.
At each step, merge the closest pair of clusters until only one cluster (or \(k\) clusters) remains.
Most commonly used in practice.
Start with one all-inclusive cluster containing every point.
At each step, split a cluster until each cluster contains a singleton (or there are \(k\) clusters).
Rarely used because of its computational complexity and the lack of a clear splitting strategy.
2.5 Agglomerative Clustering Algorithm
Compute the proximity (distance) matrix between all points.
Let each data point be its own cluster initially.
Repeat:
a) Merge the two closest clusters.
b) Update the proximity matrix to reflect the distances from the new merged cluster to the remaining clusters.
Continue until only a single cluster remains (or \(k\) clusters).
The key operation is computing the proximity between two clusters. Different ways of defining this distance lead to different Agglomerative Clustering algorithms.
2.6 Inter-Cluster Similarity: Linkage Methods
Given two clusters \( c_i \) and \( c_j \), how do we compute a single distance \( D(c_i, c_j) \) between them? Four common methods are introduced below:
Single Linkage (MIN)
Complete Linkage (MAX)
Average Linkage
Centroid Distance
Definition: Distance between two clusters is the shortest distance between any pair of points, with one point from each cluster.
\[
D(c_i, c_j) = \min_{\substack{a \in c_i \\ b \in c_j}} d(a, b)
\]
When merging \( c_k = c_i \cup c_j \), the Lance-Williams update is:
A startup with 100,000,000 customer records wants to create customer segments for marketing. Which clustering approach is more appropriate, and why?
Partitional clustering (K-Means / DBSCAN). Hierarchical clustering has
\(O(n^2)\) memory cost for the proximity matrix, which is infeasible for 100M points.
Partitional methods are generally more scalable. The flexibility of choosing K
post-hoc from a dendrogram is not useful if the hierarchical method cannot be computed at this scale.
Example 4: Linkage Intuition
Two clusters are shaped like two long, thin crescents that touch at one point. Which linkage method will merge them first?
Reveal Answer
Single Linkage (MIN). Because the touching pair has distance ≈ 0,
Single Linkage will merge the crescents even if most points in the two
crescents are far apart. This is the "chaining effect." Complete Linkage would
use the MAX distance (from one crescent tip to the opposite tip) and keep them separate.
4. Numerical Solutions
Problem 1: Agglomerative Clustering — First Merge
Given 5 points with Euclidean distance matrix:
A
B
C
D
E
A
0
3
7
9
11
B
3
0
6
8
10
C
7
6
0
2
12
D
9
8
2
0
13
E
11
10
12
13
0
Which pair is merged first in Single Linkage? What is the merge distance?
📘 Step-by-Step Solution
Step 1. Find the smallest non-zero entry in the matrix.
Step 3. Single Linkage uses the minimum distance, so the merge criterion is the smallest non-zero entry.
➡️ First merge: {C, D} at distance 2.
Problem 2: Complete Linkage After Merge
After merging C and D from Problem 1 into cluster CD = {C, D},
compute the distance between CD and the other points (A, B, E)
using Complete Linkage (MAX).
Next merge will be A-B at distance 3 (Complete Linkage).
Problem 3: Average Linkage Distance
Cluster X = {p1, p2} and Cluster Y = {q1, q2, q3}.
The matrix of pairwise Euclidean distances is:
q1
q2
q3
p1
2
4
6
p2
3
5
7
Compute the Average Linkage distance D(X, Y).
📘 Step-by-Step Solution
Step 1. Count the pairs: |X| = 2, |Y| = 3, so the total number of cross-cluster pairs is 2 × 3 = 6.
Step 2. Sum the six pairwise distances:
\[
\sum d(a,b) = 2 + 4 + 6 + 3 + 5 + 7 = 27
\]
Step 3. Divide the sum by the number of pairs:
\[
D(X,Y) = \frac{27}{6} = 4.5
\]
➡️ Average Linkage distance = 4.5.
5. Try It Yourself
Practice 3: Linkage on 3 Points
Three 1-D points are located at p1 = 0, p2 = 5, and p3 = 9. We start with the singleton clusters {p1}, {p2}, and {p3}.
State the first merge and its distance.
After the first merge, compute the distance from the new cluster to the remaining singleton under: (a) Single Linkage, (b) Complete Linkage, (c) Average Linkage.
1st merge: The pairwise distances are d(p1,p2)=5, d(p2,p3)=4, and d(p1,p3)=9. The minimum is 4, so we merge {p2, p3} at distance 4.
After this merge, we have C23 = {p2, p3} and the singleton {p1}. We now compute D(C23, {p1}) under each linkage method:
Clustering is unsupervised grouping: maximize inter-cluster distance and minimize intra-cluster distance.
Two paradigms: Partitional (K-Means, DBSCAN — non-overlapping and scalable) versus Hierarchical (dendrogram and nested clusters, generally suited to smaller datasets).
Agglomerative HC starts with singleton clusters and iteratively merges the closest pair using Single (MIN), Complete (MAX), Average, or Centroid linkage.
Single linkage is prone to chaining; Complete linkage favors compact clusters; Average linkage provides a robust middle ground between the two.
8. Common Pitfalls
Applying Agglomerative clustering to 100k+ samples: The proximity matrix requires \(O(n^2)\) memory. For large datasets, consider more scalable partitional methods.
Forgetting Lance-Williams updates: After merging two clusters, update distances using the correct linkage formula; do not re-evaluate all n² distances, which is wasteful and can lose the hierarchical property.
Mis-applying MIN formula: Single Linkage between clusters = min over ALL cross pairs, not just the cluster "representatives."